package com.visionet.waveletData.web.entity.vo;

import com.google.common.collect.Lists;
import com.visionet.waveletData.web.common.constant.SysConstants;
import com.visionet.waveletData.web.common.pingying.CnToSpell;
import com.visionet.waveletData.web.common.utilities.string.StringUtil;
import com.visionet.waveletData.web.common.utilities.time.DateUtil;
import com.visionet.waveletData.web.common.utils.BeanConvertMap;
import com.visionet.waveletData.web.common.utils.Validator;
import com.visionet.waveletData.web.entity.account.User;
import com.visionet.waveletData.web.entity.business.UserInfo;
import com.visionet.waveletData.web.service.KeyWord;
import com.visionet.waveletData.web.vo.account.UserSimpleVo;
import com.visionet.waveletData.web.vo.account.UserVo;
import com.visionet.waveletData.web.vo.business.UserInfoVo;

import java.util.Date;
import java.util.List;

/**
 * @author add by WangBo 2013-12-16
 */

public class DtoUtil {
	
	public static UserInfo convertToUser(UserVo vo) throws Exception {
		return convertToUser(null, vo);	
	}
	
	
	


	

	public static UserInfo convertToUser(UserInfo u, UserVo vo) throws Exception {
		if(u == null) {
			u = new UserInfo();
		}
		u.setId(vo.getId());
		u.setOrgId(vo.getOrgId());
		u.setOrgName(vo.getOrgName());
		u.setUserName(vo.getAliasName());
        if(vo.getIsLock()==null) {
            u.setIsLock(SysConstants.IS_SEC_UNLOCK);
        }else{
            u.setIsLock(vo.getIsLock());
        }
		u.setPhoneNumber(vo.getPhoneNumber());
		u.setLoginName(vo.getLoginName());
		//登录名默认为手机号
		if(u.getPhoneNumber() == null && u.getLoginName()!=null && StringUtil.isInteger(u.getLoginName())){
			u.setPhoneNumber(Long.valueOf(u.getLoginName()));
		}
		u.setAllowLoginFromMobile(vo.getAllowLoginFromMobile());
		u.setReceiveMessage(KeyWord.NOT_RECEIVE_MESSAGE);
		u.setUpdateDate(new Date());
		if(u.getUserName()!=null){
			u.setFirstLetter(CnToSpell.getPinYinHeadChar(u.getUserName()).toUpperCase());
			if(u.getTrueName()==null){
				u.setTrueName(u.getUserName());
				u.setTrueFirstLetter(CnToSpell.getPinYinHeadChar(u.getTrueName()).toUpperCase());
			}
		}
		
		u.setAddress(vo.getAddress());
		u.setEmail(vo.getEmail());
		if(Validator.isNotNull(vo.getBirthday())){
			try{
			u.setBirthday(DateUtil.convertFromString(vo.getBirthday(), DateUtil.YMD1));
			}catch(Exception e) {
				u.setBirthday(null);
			}
		}
		u.setWebsite(vo.getWebsite());
		u.setTitle(vo.getTitle());
		if(u.getViewPhone()==null) u.setViewPhone(KeyWord.USER_VIEW_PHONE_HIDE);
		return u;
	}
	
	public static User convertToUser(UserInfoVo vo) {
		User u = new User();
		
		u.setId(vo.getId());
		
//		u.setLoginName(vo.getLoginName());
//		u.setOrgName(vo.getOrgName());
//		u.setAliasName(vo.getUserName());
//		u.setPlainPassword(vo.getPlainPassword());
//
//		u.setOrgId(vo.getOrgId());
//		u.setOrgName(vo.getOrgName());
//		u.setIsLock(SysConstants.IS_SEC_UNLOCK);
//		u.setIsLockSec(SysConstants.IS_SEC_UNLOCK);
//		u.setLastLogin(vo.getLastLogin());
//		
//		u.setUserInfo(BeanConvertMap.map(vo,UserInfo.class));
		
		return u;
	}

	public static UserInfo convertToUser(UserInfo user, UserInfoVo vo) throws Exception {
		if(user == null) {
			user = new UserInfo();
		}
		user.setId(vo.getId());
		
		return user;
	}
	


	
	
	public static List<UserSimpleVo> UserToUserSimpleVo(List<User> userList){
		List<UserSimpleVo> voList = Lists.newArrayList();
		for(User user:userList){
			voList.add(UserToUserSimpleVo(user));
		}
		return voList;
	}
	
	
	public static UserSimpleVo UserToUserSimpleVo(User user){
		UserSimpleVo vo = new UserSimpleVo();
		
		vo.setId(user.getId());
//		vo.setOrgId(user.getOrgId());
//		vo.setAliasName(user.getAliasName());
//        vo.setLoginName(user.getLoginName());
//        vo.setIsLock(user.getIsLock());
//		
//		UserInfo ui = user.getUserInfo();
//		if(ui!=null){
//			vo.setTitle(ui.getTitle());
////			vo.setImageUrl(ui.getImageUrl());
//			vo.setPhoneNumber(ui.getPhoneNumber());
//			vo.setViewPhone(ui.getViewPhone());
//			vo.setDepartmentId(ui.getDepartmentId());
//			vo.setTrueName(ui.getTrueName());
//		}
		
		return vo;
	}
	

}

